/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.apiserver.http;

import org.openislands.oi.apiserver.app.OperatorService;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.pojo.dto.CommonResult;
import org.openislands.oi.pojo.dto.OperatorDTO;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.OperatorVO;
import org.openislands.oi.util.ValueCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/operators")
public class OperatorController {
    @Resource
    private OperatorService operatorService;

    @GetMapping("/")
    public CommonResponse<List<OperatorVO>> queryAll() {
        List<OperatorDTO> operatorDTOS = operatorService.queryAnyOperator(new OperatorDTO());
        if (operatorDTOS.isEmpty()){
            return CommonResponse.error(MessageCodeEnum.DATA_NOT_FOUND_ERROR);
        }else {
            return CommonResponse.success(ValueCopyUtils.copyList(operatorDTOS,
                    operatorDTO -> ValueCopyUtils.copyProperties(operatorDTO, new OperatorVO())));
        }
    }
}
